package jp.co.sharp.android.parents.kidsguard.activity.common;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.res.ResourcesCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.bluetooth.parentble.ParentBleTransfer;

import org.json.JSONObject;

import jp.co.sharp.android.parents.kidsguard.App;
import jp.co.sharp.android.parents.kidsguard.R;
import jp.co.sharp.android.parents.kidsguard.activity.setting.ContactOptActivity;
import jp.co.sharp.android.parents.kidsguard.activity.setting.ContactOptAddActivity;
import jp.co.sharp.android.parents.kidsguard.common.DealWithWaitingDlg;
import jp.co.sharp.android.parents.kidsguard.common.DelDeviceConfirmDlg;
import jp.co.sharp.android.parents.kidsguard.common.ProcessWarningDlg;
import jp.co.sharp.android.parents.kidsguard.contants.Constants;
import jp.co.sharp.android.parents.kidsguard.contants.PageSerialNumConstants;
import jp.co.sharp.android.parents.kidsguard.contants.RequestCodeConstants;
import jp.co.sharp.android.parents.kidsguard.contants.RequestMethodConstants;
import jp.co.sharp.android.parents.kidsguard.contants.RequestReceiverConstants;
import jp.co.sharp.android.parents.kidsguard.dto.BluetoothReceiverDto;
import jp.co.sharp.android.parents.kidsguard.dto.ContactListDto;
import jp.co.sharp.android.parents.kidsguard.entity.KidInfoEntity;
import jp.co.sharp.android.parents.kidsguard.thread.BusinessThread;
import jp.co.sharp.android.parents.kidsguard.utils.CommonUtils;

/**
 * 連絡先を追加  TODO Mimamori
 */
public class MimamoriContactAddActivity extends Activity {

    // 変わらない
    private final static int UNCHANGED = 0;
    // 一部変更
    private final static int PARTIAL_CHANGED = 1;
    // 変更
    private final static int CHANGED = 2;

    private App mApp;
    private KidInfoEntity mKidInfoEntity;
    private DealWithWaitingDlg mDealWithWaitingDlg;
    private ParentBleTransfer mParentBleTransfer;

    private ImageButton mAvatarIb;
    private ImageView mPortraitIv;
    private TextView mNameEt;
    private TextView mYomiEt;
    private TextView mPhoneEt;
    private Button mSubmitBtn;
    private Intent mIntent;
    private ContactListDto mContactListDto;
    private DelDeviceConfirmDlg mDelDeviceConfirmDlg;
    // 0: lock, 0: init , 1: getInfo , 2: setInfo, 3 :end
    private int mViewProcess = 0;
    private int mPageSerialNum;
    private boolean mScreenOperate = false;

    private boolean mUnReceiver = true;
    // ページジャンプ
    private boolean mPageJumpFlag = false;
    // システムホームプレス
    private boolean mSystemHomeFlag = false;
    private static final String TAG = "TAG";
    // プログラムロック
    private String mSyncLock = "syncLock";
    // 編集を再開しています...
    private boolean mEditReopening = false;
    private static final int PAGE_EDIT_REOPEN = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "MimamoriContactAddActivity_onCreate");
        setContentView(R.layout.activity_mimamori_contact_add);

        mApp = (App)getApplication();
        initReceiver();
        initActivity();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i(TAG, "MimamoriContactAddActivity_onRestart");
        synchronized (mSyncLock) {
            if (mSystemHomeFlag) {
                if (mApp.getAppStatus() != Constants.APP_STATUS_USER_PRESENT) {
                    Message msg = new Message();
                    msg.what = PAGE_EDIT_REOPEN;
                    handler.sendMessage(msg);
                }
                mSystemHomeFlag = false;
            }
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(this.TAG, "MimamoriContactAddActivity_onStart");
        mApp.setAppStatus(Constants.APP_STATUS_INIT);
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "MimamoriContactAddActivity_onStop");
        if (!mUnReceiver) {
            synchronized (mSyncLock) {
                try {
                    unregisterReceiver(receiver);
                } catch (IllegalArgumentException e) {
                    if (e.getMessage().contains("Receiver not registered")) {
                        // Ignore this exception. This is exactly what is desired
                        Log.e(TAG, e.getMessage());
                    } else {
                        // unexpected, re-throw
                        Log.e(TAG, e.getMessage());
                    }
                }
                if (mPageJumpFlag) {
                    try {
                        unregisterReceiver(systemReceiver);
                    } catch (IllegalArgumentException e) {
                        if (e.getMessage().contains("Receiver not registered")) {
                            // Ignore this exception. This is exactly what is desired
                            Log.e(TAG, e.getMessage());
                        } else {
                            // unexpected, re-throw
                            Log.e(TAG, e.getMessage());
                        }
                    }
                    mPageJumpFlag = false;
                }
            }
            mUnReceiver = true;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "MimamoriContactAddActivity_onDestroy");
        if (!mUnReceiver) {
            synchronized (mSyncLock) {
                try {
                    unregisterReceiver(receiver);
                } catch (IllegalArgumentException e) {
                    if (e.getMessage().contains("Receiver not registered")) {
                        // Ignore this exception. This is exactly what is desired
                        Log.e(TAG, e.getMessage());
                    } else {
                        // unexpected, re-throw
                        Log.e(TAG, e.getMessage());
                    }
                }
                if (mPageJumpFlag) {
                    try {
                        unregisterReceiver(systemReceiver);
                    } catch (IllegalArgumentException e) {
                        if (e.getMessage().contains("Receiver not registered")) {
                            // Ignore this exception. This is exactly what is desired
                            Log.e(TAG, e.getMessage());
                        } else {
                            // unexpected, re-throw
                            Log.e(TAG, e.getMessage());
                        }
                    }
                    mPageJumpFlag = false;
                }
            }
            mUnReceiver = true;
        }
    }

    private void initActivity() {
        Log.i(TAG, "ContactOptAddActivity_initActivity");
        this.mIntent = getIntent();
        mPageSerialNum = mIntent.getIntExtra(Constants.KEY_PAGE_SERIAL_NUM, PageSerialNumConstants.PAGE_SERIAL_CONTACT);
        this.mKidInfoEntity = mApp.findKidInfoBySelect();

        TextView textView = (TextView)findViewById(R.id.settingSecondMenu);
        String strSecondMenu = mKidInfoEntity.getKidName() + "のキッズケータイ";
        textView.setText(strSecondMenu);

        mPortraitIv = (ImageView) findViewById(R.id.imageViewPortrait);
        mAvatarIb = (ImageButton)findViewById(R.id.editAvatarBtn);
        mNameEt = (TextView)findViewById(R.id.editTextName);
        mYomiEt = (TextView)findViewById(R.id.editTextYomi);
        mPhoneEt = (TextView)findViewById(R.id.editTextPhone);
        mSubmitBtn = (Button)findViewById(R.id.submitBtn);
        mSubmitBtn.setEnabled(false);
        mSubmitBtn.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.button_reflect_normal, null));

        mContactListDto = new ContactListDto();
        mContactListDto.setId(-1);
        mNameEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (checkSubmit() == CHANGED) {
                    mSubmitBtn.setEnabled(true);
                    mSubmitBtn.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.button_reflect_change, null));
                } else {
                    mSubmitBtn.setEnabled(false);
                    mSubmitBtn.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.button_reflect_normal, null));
                }
                if (!TextUtils.isEmpty(mNameEt.getText())) {
                    mContactListDto.setName(mNameEt.getText().toString());
                }
            }
        });
        mYomiEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (checkSubmit() == CHANGED) {
                    mSubmitBtn.setEnabled(true);
                    mSubmitBtn.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.button_reflect_change, null));
                } else {
                    mSubmitBtn.setEnabled(false);
                    mSubmitBtn.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.button_reflect_normal, null));
                }
                if (!TextUtils.isEmpty(mYomiEt.getText())) {
                    mContactListDto.setYomi(mYomiEt.getText().toString());
                }
            }
        });
        mPhoneEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (checkSubmit() == CHANGED) {
                    mSubmitBtn.setEnabled(true);
                    mSubmitBtn.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.button_reflect_change, null));
                } else {
                    mSubmitBtn.setEnabled(false);
                    mSubmitBtn.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.button_reflect_normal, null));
                }
                if (!TextUtils.isEmpty(mPhoneEt.getText())) {
                    mContactListDto.setPhone(mPhoneEt.getText().toString());
                }
            }
        });
        mAvatarIb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MimamoriContactAddActivity.this, MimamoriContactAvatarActivity.class);
                intent.putExtra(Constants.KEY_PAGE_SERIAL_NUM, mPageSerialNum);
                startActivityForResult(intent, RequestCodeConstants.MIMAMORI_CONTACT_ADD_AVATAR);
            }
        });

        mSubmitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mSubmitBtn.isEnabled()) {
                    // Dlg
                    String message = "キッズケータイへ反映中..";
                    mDealWithWaitingDlg = new DealWithWaitingDlg(MimamoriContactAddActivity.this, R.style.commonDlg, message, true);
                    mDealWithWaitingDlg.show();

                    sendSetToKid();
                }
            }
        });
        mViewProcess = Constants.SEND_KID_IS_USING;
        new BusinessThread(Constants.SEND_KID_IS_USING, mParentBleTransfer, mKidInfoEntity, null, mPageSerialNum).start();
    }

    /**
     * 初期化Receiver
     */
    private void initReceiver() {
        Log.i(TAG, "MimamoriContactAddActivity_initReceiver");
        IntentFilter filter = new IntentFilter();
        // ロック画面
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        // 画面を開く
        filter.addAction(Intent.ACTION_USER_PRESENT);
        // ホームボタン
        filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        filter.addAction(RequestReceiverConstants.RECEIVER_BLUETOOTH_CONNECT_STATE_CHANGED);
        filter.addAction(RequestReceiverConstants.RECEIVER_BLUETOOTH_WRITE_DATA_CHANGED);
        filter.addAction(RequestReceiverConstants.RECEIVER_BLUETOOTH_LISTENER_DATA_CHANGED);
        registerReceiver(receiver, filter);
        registerReceiver(systemReceiver, filter);
        this.mParentBleTransfer = ((App)getApplication()).getmParentBleTransfer();
        mUnReceiver = false;
    }

    /**
     *
     * @return 0: 変わらない 1: 一部変更 2: 変更
     */
    private int checkSubmit() {
        Log.i(TAG, "MimamoriContactAddActivity_checkSubmit");
        if (TextUtils.isEmpty(mNameEt.getText()) &&
                TextUtils.isEmpty(mYomiEt.getText()) &&
                TextUtils.isEmpty(mPhoneEt.getText())) {
            return 0;
        }
        if (TextUtils.isEmpty(mNameEt.getText()) ||
                TextUtils.isEmpty(mYomiEt.getText()) ||
                TextUtils.isEmpty(mPhoneEt.getText())) {
            return 1;
        }
        return 2;
    }


    private void sendSetToKid() {
        Log.i(TAG, "MimamoriContactAddActivity_sendSetToKid");
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("method", RequestMethodConstants.METHOD_ADD_CONTACTS);
            JSONObject jsonData = new JSONObject();
            jsonData.put("id", mContactListDto.getId());
            jsonData.put("name", mContactListDto.getName());
            jsonData.put("phone", mContactListDto.getPhone());
            jsonData.put("yomi", mContactListDto.getYomi());
            jsonData.put("portrait", mContactListDto.getPortrait());
            jsonObject.put("data", jsonData);

            mViewProcess = Constants.SEND_KID_SET_INFO;
            new BusinessThread(Constants.SEND_KID_SET_INFO, mParentBleTransfer, mKidInfoEntity, jsonObject, mPageSerialNum).start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * システム放送
     */
    private final BroadcastReceiver systemReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                String action = intent.getAction();
                if (Intent.ACTION_SCREEN_OFF.equals(action)) {
                    synchronized (mSyncLock) {
                        mApp.setAppStatus(Constants.APP_STATUS_SCREEN_OFF);
                        mViewProcess = Constants.SEND_KID_EXIT;
                        new BusinessThread(Constants.SEND_KID_EXIT,
                                mParentBleTransfer,
                                mKidInfoEntity,
                                null,
                                mPageSerialNum).start();
                    }
                } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
                    synchronized (mSyncLock) {
                        mApp.setAppStatus(Constants.APP_STATUS_USER_PRESENT);
                        if (!mPageJumpFlag) {
                            Message msg = new Message();
                            msg.what = PAGE_EDIT_REOPEN;
                            handler.sendMessage(msg);
                        }
                    }
                } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
                    synchronized (mSyncLock) {
                        mApp.setAppStatus(Constants.APP_STATUS_CLOSE_SYSTEM_DIALOGS);
                        mSystemHomeFlag = true;
                        mViewProcess = Constants.SEND_KID_EXIT;
                        new BusinessThread(Constants.SEND_KID_EXIT,
                                mParentBleTransfer,
                                mKidInfoEntity,
                                null,
                                mPageSerialNum).start();
                    }
                }
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }
        }
    };
    /**
     * BroadcastReceiver
     */
    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                String action = intent.getAction();
                if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
                    int blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
                    if (BluetoothAdapter.STATE_OFF == blueState) {
                        mKidInfoEntity.setBtConnStatus(Constants.DEVICE_DISCONNECTION_SUCCESS);
                        mApp.updateKidInfo(mKidInfoEntity);

                        String message = mKidInfoEntity.getKidName() + "と切断されました。編集を中断します。";
                        ProcessWarningDlg processWarringDlg = new ProcessWarningDlg(MimamoriContactAddActivity.this, R.style.commonDlg, message);
                        processWarringDlg.show();
                        processWarringDlg.setOnOperateSetListener(new ProcessWarningDlg.OnOperateSetListener() {
                            @Override
                            public void onOperateSet() {
                                processWarringDlg.dismiss();

                                backActivity();
                            }
                        });
                    }
                } else if (RequestReceiverConstants.RECEIVER_BLUETOOTH_WRITE_DATA_CHANGED.equals(action)) {
                    BluetoothReceiverDto bluetoothReceiverDto = (BluetoothReceiverDto) intent.getSerializableExtra(RequestReceiverConstants.RECEIVER_KEY);
                    if (bluetoothReceiverDto.isFinished()) {
                        switch(mViewProcess) {
                            case Constants.SEND_KID_IS_USING:
                                break;
                            case Constants.SEND_KID_ENTER:
                                mEditReopening = false;
                                mScreenOperate = false;
                                handler.sendEmptyMessage(0);
                                break;
                            case Constants.SEND_KID_GET_INFO:
                                break;
                            case Constants.SEND_KID_SET_INFO:
                                break;
                            case Constants.SEND_KID_EXIT:
                                if (!mScreenOperate) {
                                    backActivity();
                                }
                                break;
                        }
                    }
                } else if (RequestReceiverConstants.RECEIVER_BLUETOOTH_LISTENER_DATA_CHANGED.equals(action)) {
                    BluetoothReceiverDto bluetoothReceiverDto = (BluetoothReceiverDto) intent.getSerializableExtra(RequestReceiverConstants.RECEIVER_KEY);
                    String retStr = bluetoothReceiverDto.getRetStr();
                    JSONObject jsonObject = new JSONObject(retStr);
                    if (RequestMethodConstants.METHOD_ON_SYNC_RESULT.equals(jsonObject.getString("method"))) {
                        if (jsonObject.getString("result").equals(Constants.ON_SYNC_RESULT)) {
                            handler.sendEmptyMessage(0);
                            backActivity(null);
                        } else {
                            handler.sendEmptyMessage(0);
                            String message = "キッズケータイへ反映できませんでした";
                            ProcessWarningDlg processWarringDlg = new ProcessWarningDlg(MimamoriContactAddActivity.this, R.style.commonDlg, message);
                            processWarringDlg.show();
                            processWarringDlg.setOnOperateSetListener(new ProcessWarningDlg.OnOperateSetListener() {
                                @Override
                                public void onOperateSet() {
                                    processWarringDlg.dismiss();
                                }
                            });
                        }
                    } else if (RequestMethodConstants.METHOD_ON_OPERATE_INTERRUPT.equals(jsonObject.getString("method"))) {
                        String message = "キッズケータイが操作されました。編集を中断します。";
                        ProcessWarningDlg processWarringDlg = new ProcessWarningDlg(MimamoriContactAddActivity.this, R.style.commonDlg, message);
                        processWarringDlg.show();
                        processWarringDlg.setOnOperateSetListener(new ProcessWarningDlg.OnOperateSetListener() {
                            @Override
                            public void onOperateSet() {
                                processWarringDlg.dismiss();
                                backActivity();
                            }
                        });
                    } else if (RequestMethodConstants.METHOD_GET_OPERATING_STATE.equals(jsonObject.getString("method"))) {
                        if (jsonObject.getJSONObject("data").getString("state").equals(Constants.IS_USING_OK)) {
                            mViewProcess = Constants.SEND_KID_ENTER;
                            new BusinessThread(Constants.SEND_KID_ENTER, mParentBleTransfer, mKidInfoEntity, null, mPageSerialNum).start();
                        } else {
                            handler.sendEmptyMessage(0);
                            String message = "別のスマートフォンから操作中のため、設定を変更できません。";
                            ProcessWarningDlg processWarringDlg = new ProcessWarningDlg(MimamoriContactAddActivity.this, R.style.commonDlg, message);
                            processWarringDlg.show();
                            processWarringDlg.setOnOperateSetListener(new ProcessWarningDlg.OnOperateSetListener() {
                                @Override
                                public void onOperateSet() {
                                    processWarringDlg.dismiss();
                                    backActivity();
                                }
                            });
                        }
                    }
                }
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }
        }
    };
    /**
     * Dialog閉じる
     */
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (0 == msg.what) {
                if (null != mDealWithWaitingDlg && mDealWithWaitingDlg.isShowing()) {
                    mDealWithWaitingDlg.dismiss();
                }
            } else if (msg.what == PAGE_EDIT_REOPEN) {
                if (!mEditReopening) {
                    // 非ページジャンプ編集が再び開きます
                    String message = "編集を再開しています...";
                    mDealWithWaitingDlg = new DealWithWaitingDlg(MimamoriContactAddActivity.this, R.style.commonDlg, message, false);
                    mDealWithWaitingDlg.show();
                    mViewProcess = Constants.SEND_KID_IS_USING;
                    new BusinessThread(Constants.SEND_KID_IS_USING,
                            mParentBleTransfer,
                            mKidInfoEntity,
                            null,
                            mPageSerialNum).start();
                    // ブロードキャストの再登録
                    unregisterReceiver(systemReceiver);
                    initReceiver();
                    mEditReopening = true;
                }
            }
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        Log.i(TAG, "MimamoriContactAddActivity_onActivityResult");
        if (requestCode == RequestCodeConstants.MIMAMORI_CONTACT_ADD_AVATAR) {
            // ブロードキャストの再登録
            initReceiver();
            if (null != intent && intent.hasExtra(Constants.KEY_CONTACT_ADD_AVATAR) && null != intent.getStringExtra(Constants.KEY_CONTACT_ADD_AVATAR)) {
                mPortraitIv.setImageResource(CommonUtils.chooseImage(Integer.valueOf(intent.getStringExtra(Constants.KEY_CONTACT_ADD_AVATAR))));
                mContactListDto.setPortrait(intent.getStringExtra(Constants.KEY_CONTACT_ADD_AVATAR));
            } else if (null != intent && intent.hasExtra(Constants.KEY_PAGE_ERROR) && null != intent.getStringExtra(Constants.KEY_PAGE_ERROR)) {
                backActivity();
            }
        }
    }

    /**
     * ページリターンキー、フルスクリーンリターンジェスチャー
     */
    @Override
    public void onBackPressed() {
        backActivity(null);
    }

    /**
     * 前のページに戻る(値なし)
     */
    public void backActivity(View view) {
        Log.i(TAG, "MimamoriContactAddActivity_backActivity");
        if (view != null) {
            if (checkSubmit() == CHANGED || checkSubmit() == PARTIAL_CHANGED) {
                String message = "編集内容は保存されていません。編集内容を破棄して前の画面に戻りますか？";
                mDelDeviceConfirmDlg = new DelDeviceConfirmDlg(MimamoriContactAddActivity.this, R.style.commonDlg, message);
                mDelDeviceConfirmDlg.show();
                mDelDeviceConfirmDlg.setOnOperateSetListener(new DelDeviceConfirmDlg.OnOperateSetListener() {
                    @Override
                    public void onOperateSet() {
                        setResult(RequestCodeConstants.MIMAMORI_CONTACT_ADD, mIntent);
                        // ページジャンプ
                        mPageJumpFlag = true;
                        finish();
                    }
                });
            } else {
                mViewProcess = Constants.SEND_KID_EXIT;
                new BusinessThread(Constants.SEND_KID_EXIT,
                        mParentBleTransfer,
                        mKidInfoEntity,
                        null,
                        mPageSerialNum).start();
//                setResult(RequestCodeConstants.MIMAMORI_CONTACT_ADD, mIntent);
//                // ページジャンプ
//                mPageJumpFlag = true;
//                finish();
            }
        } else {
            mViewProcess = Constants.SEND_KID_EXIT;
            new BusinessThread(Constants.SEND_KID_EXIT,
                    mParentBleTransfer,
                    mKidInfoEntity,
                    null,
                    mPageSerialNum).start();
        }
    }

    private void backActivity() {
        Log.i(TAG, "MimamoriContactAddActivity_backActivity");
        setResult(RequestCodeConstants.MIMAMORI_CONTACT_ADD, mIntent);
        // ページジャンプ
        mPageJumpFlag = true;
        finish();
    }
}
